package cn.stylefeng.guns.modular.hp.controller;

import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.hp.domain.HospitalDescInfoDO;
import cn.stylefeng.guns.modular.hp.model.HospitalDescInfo;
import cn.stylefeng.guns.modular.hp.model.PageContentInfo;
import cn.stylefeng.guns.modular.hp.service.IHospitalDescInfoService;
import cn.stylefeng.guns.modular.hp.service.IPageContentInfoService;
import cn.stylefeng.guns.modular.hp.warpper.HospitalDescInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 医院简介控制器
 *
 * @author xzqiang
 * @date 2018-12-12
 */
@Controller
@RequestMapping("/hp/hospitalDesc")
public class HospitalDescInfoController extends BaseController {

    private String PREFIX = "/hp/hospitaldesc/";

    @Autowired
    private HpProperties hpProperties;
    
    @Autowired
    private IHospitalDescInfoService hospitalDescInfoService;
    
    @Autowired
    private IPageContentInfoService pageContentInfoService;
    
    /**
     * 跳转到医院简介列表首页
     */
    @RequestMapping("")
    public String index() {
    	String htmlPath = "hospitalDescInfo.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到添加医院简介
     */
    @RequestMapping("/hospitalDesc_add")
    public String hospitalDescAdd() {
    	String htmlPath = "hospitalDescInfo_add.html";
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到修改医院简介
     */
    @RequestMapping("/hospitalDesc_update/{infoId}")
    public String hospitalDescUpdate(@PathVariable String infoId, Model model) {
    	HospitalDescInfo info = this.hospitalDescInfoService.selectById(infoId);
    	PageContentInfo pageContent = this.pageContentInfoService.selectById(info.getPageContentId());
    	
    	HospitalDescInfoDO infoDO = new HospitalDescInfoDO();
    	
    	infoDO.setId(info.getId());
    	infoDO.setPicName(info.getPicName());
    	infoDO.setPicUrl(info.getPicUrl());
        infoDO.setTitle(pageContent.getTitle());
    	infoDO.setContent(pageContent.getContent());
    	infoDO.setShowFlag(info.getShowFlag());
    	infoDO.setCreatedTime(info.getCreatedTime());
    	
        model.addAttribute("info", infoDO);
        LogObjectHolder.me().set(info);

        String htmlPath = "hospitalDescInfo_edit.html";
    	
        return PREFIX + htmlPath;
    }


    /**
     * 获取医院简介列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<Map<String, Object>> list = this.hospitalDescInfoService.list();
        return super.warpObject(new HospitalDescInfoWrapper(list));
    }

    /**
     * 新增医院简介
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增医院简介", key = "id")//, dict = NoticeMap.class
    public Object add(HospitalDescInfoDO infoDO) {
        if (ToolUtil.isOneEmpty(infoDO, infoDO.getShowFlag(),infoDO.getTitle(), infoDO.getPicUrl(), infoDO.getContent(),infoDO.getDesc())) { //, topMenuInfo.getPageContentId()
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        //新增页面内容
        PageContentInfo pageContent = new PageContentInfo();
        pageContent.setTitle(infoDO.getTitle());
        pageContent.setContent(infoDO.getContent());
        pageContent.setCreater(ShiroKit.getUser().getId());
        pageContent.setCreatedTime(new Date());
        pageContent.insert();
        
        HospitalDescInfo newHospitalDescInfo = new HospitalDescInfo();
        newHospitalDescInfo.setDesc(infoDO.getDesc());
        newHospitalDescInfo.setPicName(infoDO.getPicUrl());
        newHospitalDescInfo.setPicUrl(infoDO.getPicUrl());
        newHospitalDescInfo.setPageContentId(pageContent.getId());
        newHospitalDescInfo.setLinkUrl(pageContent.getId());
        newHospitalDescInfo.setShowFlag(infoDO.getShowFlag());
        newHospitalDescInfo.setCreater(ShiroKit.getUser().getId());
        newHospitalDescInfo.setCreatedTime(new Date());
        newHospitalDescInfo.insert();
        
        return SUCCESS_TIP;
    }

    /**
     * 删除医院简介
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除医院简介", key = "id")
    public Object delete(@RequestParam String id) {

        this.hospitalDescInfoService.deleteById(id);

        return SUCCESS_TIP;
    }

    /**
     * 修改医院简介
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改医院简介")
    public Object update(HospitalDescInfoDO infoDO) {
    	if (ToolUtil.isOneEmpty(infoDO, infoDO.getShowFlag(), infoDO.getTitle(), infoDO.getPicUrl(), infoDO.getContent(),infoDO.getDesc())) { //, topMenuInfo.getPageContentId()
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	HospitalDescInfo oldHospitalDescInfo = this.hospitalDescInfoService.selectById(infoDO.getId());
    	PageContentInfo oldPageContent = this.pageContentInfoService.selectById(oldHospitalDescInfo.getPageContentId());

        oldPageContent.setTitle(infoDO.getTitle());
        oldPageContent.setContent(infoDO.getContent());
    	oldPageContent.updateById();
        
    	oldHospitalDescInfo.setDesc(infoDO.getDesc());
    	oldHospitalDescInfo.setPicName(infoDO.getPicUrl());
    	oldHospitalDescInfo.setPicUrl(infoDO.getPicUrl());
    	oldHospitalDescInfo.setPageContentId(oldPageContent.getId());
    	oldHospitalDescInfo.setLinkUrl(oldPageContent.getId());
    	oldHospitalDescInfo.setShowFlag(infoDO.getShowFlag());
        oldHospitalDescInfo.updateById();
        
        return SUCCESS_TIP;
    }

}
